package com.prototype.demo04;

import lombok.Data;

import java.io.*;
import java.time.LocalDateTime;

@Data
public class Monkey implements Cloneable, Serializable {
    private LocalDateTime birthday;
    private GoldRingedStaff staff;
    public Monkey(){
        this.birthday = LocalDateTime.now();
        this.staff = new GoldRingedStaff();
    }

    // 深克隆
    public Object deepClone() {
        Monkey copyMonkey;
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(this);

            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
            ObjectInputStream oi = new ObjectInputStream(bi);
            copyMonkey = (Monkey) oi.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            copyMonkey = null;
        }
        return copyMonkey;
    }
}
